package view;

import app.App;
import component.JPanelWithBackground;
import model.User;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;
import java.io.IOException;

public class LoginView extends JFrame{

    private final static int INIT_W = 350;  //窗体初始宽度
    private final static int INIT_H = 300+154;  //窗体初始高度

    private JPanel p_login;
    private JFormattedTextField tf_account;
    private JPasswordField pf_password;
    private JButton btn_login;
    private JPanel headImage;

    public LoginView() {
        try {
            headImage.setLayout(new GridLayout(1,1));
            headImage.add(new JPanelWithBackground("src/image/background.jpg"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("登录");
        setSize(new Dimension(INIT_W,INIT_H));
        setResizable(false);
        setContentPane(p_login);
        setVisible(true);
        //居中
        Point p = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
        setBounds(p.x - INIT_W / 2, p.y - INIT_H / 2, INIT_W, INIT_H);

        //输入格式限制
        //NumberFormat nf = NumberFormat.getIntegerInstance();
        //NumberFormatter nff = new NumberFormatter(nf);
        //DefaultFormatterFactory factory = new DefaultFormatterFactory(nff);
        //tf_account.setFormatterFactory(factory);

        btn_login.setEnabled(false);//默认禁用登录按钮

        initComponents();//初始化组件
    }

    void initComponents() {
        //账号输入框没有字符时禁用登录按钮
        tf_account.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent e) {
                setBtnLoginState();
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                setBtnLoginState();
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                setBtnLoginState();
            }

            void setBtnLoginState() {
                btn_login.setEnabled(tf_account.getText().length() > 0);
            }
        });

        //密码输入框没有字符时禁用登录按钮
        pf_password.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent e) {
                setBtnLoginState();
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                setBtnLoginState();
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                setBtnLoginState();
            }

            void setBtnLoginState() {
                btn_login.setEnabled(pf_password.getPassword().length > 0);
            }
        });

        //登录按钮监听
        btn_login.addActionListener(e -> {
            User inputUser = User.checkUser(tf_account.getText(), String.valueOf(pf_password.getPassword()));
            if (inputUser != null) {
                //登录成功并跳转
                App.loggedUser = inputUser;//设置当前用户
                this.dispose();//关闭当前窗口
                new ManageView();//启动管理界面
            }
            else {
                JOptionPane.showMessageDialog(p_login, "登陆失败！", null, JOptionPane.ERROR_MESSAGE);
            }
        });
    }
}
